package com.ruoyi.algorithm.dict.repository;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.algorithm.dict.entity.Dictionary;
import com.ruoyi.algorithm.dict.entity.DictionaryType;
import com.ruoyi.algorithm.mapper.DictionaryMapper;
import com.ruoyi.algorithm.mapper.DictionaryTypeMapper;
import com.beihu.sightplus.common.repository.BaseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ：yanfa-03
 * @date ：Created in 2021/7/15/10:34
 * @description ：字典表repo
 * @version: 1.0
 */
@Service
public class DictionaryRepository extends BaseRepository<DictionaryMapper, Dictionary> {

    @Autowired
    DictionaryMapper dictionaryMapper;
    @Autowired
    DictionaryTypeMapper dictionaryTypeMapper;

    /**
     * 根据字典类型表key与字典表key获取字典表value
     *
     * @param typeKey 字典类型表key
     * @param key     字典表key
     * @return 字典表value
     */
    public String getValueBykey(String typeKey, String key) {
        DictionaryType dictionaryType = dictionaryTypeMapper.selectOne(Wrappers.lambdaQuery(DictionaryType.class)
                .eq(DictionaryType::getTypeKey, typeKey)
        );
        if (ObjectUtils.isNotEmpty(dictionaryType)) {
            Long dictionaryTypeId = dictionaryType.getId();
            Dictionary dictionary = dictionaryMapper.selectOne(Wrappers.lambdaQuery(Dictionary.class)
                    .eq(Dictionary::getTypeKeyId, dictionaryTypeId)
                    .eq(Dictionary::getKey, key)
            );
            if (ObjectUtils.isNotEmpty(dictionary)) {
                return dictionary.getValue();
            }
        }
        return "null";
    }
}
